﻿#include "OssOperate.h"

OssOperate::OssOperate()
{
	// 初始化OSS账号信息
	m_accessKeyId = "";
	m_accessKeySecret = "";
	m_endpoint = "oss-cn-beijing.aliyuncs.com";
	m_bucketName = "mindx-media";

	// yourObjectName表示上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如test/123.png
	std::string ObjectName = "test/12.jpg";

	// 初始化OSS客户端
	ClientConfiguration conf;
	m_client = new OssClient(m_endpoint, m_accessKeyId, m_accessKeySecret, conf);

	// 初始化网络等资源
	InitializeSdk();
}

// 创建存储空间
bool OssOperate::createBucket()
{
	// 指定新创建bucket的名称、存储类型和ACL
	CreateBucketRequest request(m_bucketName, StorageClass::IA, CannedAccessControlList::PublicReadWrite);

	// 创建bucket
	auto outcome = m_client->CreateBucket(request);

	if (!outcome.isSuccess()) {
		// 异常处理
		std::cout << "CreateBucket fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return false;
	}

	// 释放网络等资源
	ShutdownSdk();

	return true;
}

// 上传文件
bool OssOperate::upload(std::string objectName, std::string localName)
{
	// yourLocalFilename由本地文件路径加文件名包括后缀组成，例如C:\\Users\\feng\\Desktop\\123.png
	auto outcome = m_client->PutObject(m_bucketName, objectName, localName);
	// 异常处理
	if (!outcome.isSuccess()) {
		std::cout << "PutObject fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return false;
	}

	// 释放网络等资源
	ShutdownSdk();

	return true;
}

// 下载文件
bool OssOperate::download(std::string objectName, std::string nameToSave)
{
	// 获取文件到本地文件
	DownloadObjectRequest request(m_bucketName, objectName, nameToSave);
	auto outcome = m_client->ResumableDownloadObject(request); // 官方的下载代码报错，这里改写了

	if (outcome.isSuccess()) {
		std::cout << "GetObjectToFile success, ContentLength is" << outcome.result().Metadata().ContentLength() << std::endl;
	}
	else {
		// 异常处理
		std::cout << "GetObjectToFile fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return false;
	}

	// 释放网络等资源
	ShutdownSdk();

	return true;
}

// 删除文件
bool OssOperate::delFile(std::string objectName)
{
	// 删除操作
	DeleteObjectRequest request(m_bucketName, objectName);
	auto outcome = m_client->DeleteObject(request);

	if (!outcome.isSuccess()) {
		// 异常处理
		std::cout << "DeleteObject fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return -1;
	}

	// 释放网络等资源
	ShutdownSdk();

	return true;
}

// 列举文件
bool OssOperate::listObjects()
{
	ListObjectsRequest request(m_bucketName);
	auto outcome = m_client->ListObjects(request);

	if (!outcome.isSuccess()) {
		// 异常处理
		std::cout << "ListObjects fail" <<
			",code:" << outcome.error().Code() <<
			",message:" << outcome.error().Message() <<
			",requestId:" << outcome.error().RequestId() << std::endl;
		ShutdownSdk();
		return false;
	}
	else {
		for (const auto& object : outcome.result().ObjectSummarys()) {
			std::cout << "object" <<
				",name:" << object.Key() <<
				",size:" << object.Size() <<
				",last modified time:" << object.LastModified() << std::endl;
		}
	}

	// 释放网络等资源
	ShutdownSdk();

	return true;
}